import { IBehaviour } from "./interface/IBehaviour";
import { BehaviorTree } from "./BehaviorTree";

export class BehaviorTreeBuilder {
    private behaviourStack: Array<IBehaviour>  = [];
    private treeRoot: IBehaviour = null;

    public addBehaviour(behaviour: IBehaviour): BehaviorTreeBuilder {
        //assert(NewBehavior);
        //如果没有根节点设置新节点为根节点
        //否则设置新节点为堆栈顶部节点的子节点
        if (this.treeRoot == null) {
            this.treeRoot = behaviour;
        } else {
            this.behaviourStack[this.behaviourStack.length - 1].addChild(behaviour);
        }

        this.behaviourStack.push(behaviour);
        return this;
    }

    public back(): BehaviorTreeBuilder {
        this.behaviourStack.pop();
        return this;
    }

    public end(): BehaviorTree {
        this.behaviourStack = [];
        let tmp: BehaviorTree = new BehaviorTree(this.treeRoot);
        return tmp;
    }

}